package com.lanxin.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxin.mp.entity.MpCardtype;
import com.lanxin.mp.service.IMpCardtypeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;
import java.util.Map;

/**
 * 名片类型控制器（仅负责参数接收与Service调用）
 */
@Controller
@RequestMapping("/mp/cardtype")
public class MpCardtypeController {

    private static final Logger logger = Logger.getLogger(MpCardtypeController.class);

    @Autowired
    private IMpCardtypeService mpCardtypeService;

    /**
     * 分页查询列表（含搜索）
     */
    @RequestMapping("/listMpCardtype")
    public String listCardtype(Model model,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String type,
                                @SessionAttribute("empOid")Integer eid) {
        // 仅接收参数，调用Service获取分页数据
        IPage<MpCardtype> pageInfo = mpCardtypeService.getCardtypesByPage(eid,page, pageSize, type);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("searchType", type); // 回显搜索条件在文本输入框
        return "mp/listMpCardtype";
    }

    /**
     * 删除名片类型
     */
    @RequestMapping("/deleteMpCardtype")
    @ResponseBody
    public boolean deleteMpCardtype(@RequestParam Integer oid) {
        // 仅传参给Service，返回结果
        boolean i = mpCardtypeService.removeById(oid);
        return i;
    }

    /**
     * 跳转到添加名片类型页面
     */
    @RequestMapping("/toAddMpCardtype")
    public String toAddMpCardtype() {
        return "mp/AddMpCardtype";
    }

    /**
     * 执行新增
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("/addMpCardtype")
    public String addMpCardtype(MpCardtype mpCardtype) {
        // 接收实体参数，调用Service保存
        boolean result = mpCardtypeService.save(mpCardtype);
        return "redirect:/mp/cardtype/listMpCardtype"; // 重定向到列表名片类型
    }

    /**
     * 跳转修改页面
     */
    @RequestMapping("/toUpdateMpCardtype")
    public String toUpdateMpCardtype(Model model, @RequestParam Integer oid) {
        // 调用Service查询单条数据，传递到页面
        MpCardtype cardtype = mpCardtypeService.getById(oid);
        model.addAttribute("mpCardtype", cardtype);
        return "mp/UpdateMpCardtype";
    }

    /**
     * 执行修改
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("/updateMpCardtype")
    public String updateMpCardtype(MpCardtype mpCardtype) {
        // 接收实体参数，调用Service更新
        boolean result = mpCardtypeService.updateById(mpCardtype);
        return "redirect:/mp/cardtype/listMpCardtype"; // 重定向到列表页
    }

    /**
     * 校验类型名称是否已经存在
     */
    @RequestMapping("/checkMpCardtypeName")
    @ResponseBody
    public Map<String, Boolean> checkMpCardtypeName(
            @RequestParam String name,
            @RequestParam(required = false) Integer oid) {
        // 传递参数给Service校验，返回结果
        boolean exists = mpCardtypeService.checkNameExists(name, oid);
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", !exists); // valid为true表示可用
        return result;
    }

}